package com.mf.controller;


import com.mf.entity.bo.BasicDataDictionaryBo;
import com.mf.entity.request.searchParams.SearchDictionaryRequest;
import com.mf.entity.vo.BasicDataDictionaryVo;
import com.mf.service.IBasicDataDictionaryService;
import io.github.heollhai.common.entity.request.publicRequest.PublicUpdateStatusRequest;
import io.github.heollhai.common.result.Result;
import io.github.heollhai.common.utils.tree.TreeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 数据字典
 *
 * @author mf
 * @since 2024-08-23
 */
@RestController
@RequestMapping("basic/basicDataDictionary/v1")
@Tag(name = "数据字典")
public class BasicDataDictionaryController {
    @Resource
    private IBasicDataDictionaryService basicDataDictionaryService;


    @Operation(summary = "新增|编辑数据字典")
    @PostMapping("save")
    public Result<Integer> save(@Valid @RequestBody BasicDataDictionaryVo basicDataDictionaryVo) {
        return Result.success(basicDataDictionaryService.insert(basicDataDictionaryVo));
    }

    @Operation(summary = "修改状态")
    @PostMapping("updateStatus")
    public Result<Boolean> updateStatus(@RequestBody PublicUpdateStatusRequest<Integer> updateStatusRequest) {
        BasicDataDictionaryBo basicDataDictionaryBo = new BasicDataDictionaryBo();
        BeanUtils.copyProperties(updateStatusRequest, basicDataDictionaryBo);
        return Result.success(basicDataDictionaryService.updateById(basicDataDictionaryBo));
    }

    @Operation(summary = "删除字典")
    @GetMapping("delete/{id}")
    public Result<Integer> deleteById(@PathVariable("id") Integer id) {
        return Result.success(basicDataDictionaryService.deleteById(id));
    }

    @Operation(summary = "查询字典")
    @PostMapping("list")
    public Result<List<BasicDataDictionaryVo>> list(@RequestBody SearchDictionaryRequest searchDictRequest) {
        return Result.success(basicDataDictionaryService.listByDictionary(searchDictRequest));
    }

    @Operation(summary = "查询字典(树结构)")
    @GetMapping("findTreeList")
    public Result<List<BasicDataDictionaryVo>> findTreeList() {
        return Result.success(TreeUtils.getRoot(basicDataDictionaryService.findDictByRoleIdList()));
    }

    @Operation(summary = "查询平铺后字典")
    @GetMapping("flattenTree")
    public Result<List<BasicDataDictionaryVo>> flattenTree() {
        final List<BasicDataDictionaryVo> roleIdList = basicDataDictionaryService.findDictByRoleIdList();
        return Result.success(TreeUtils.flattenTree(roleIdList));
    }

    @Operation(summary = "根据code查询一层子数据")
    @GetMapping("findByCode")
    public Result<Map<String, List<BasicDataDictionaryVo>>> findByCode(
            @RequestParam("pidCode") String pidCode,
            @RequestParam(name = "isRole", defaultValue = "false") Boolean isRole
    ) {
        return Result.success(basicDataDictionaryService.findByPidCode(pidCode,isRole));
    }

    @Operation(summary = "根据id查询所有子集数据")
    @GetMapping("findTreeById")
    public Result<BasicDataDictionaryVo> findTreeById(@RequestParam("id") Integer id) {
        List<BasicDataDictionaryVo> root = TreeUtils.getRoot(basicDataDictionaryService.findDictByRoleIdList());
        return Result.success(TreeUtils.findNodeById(root, id));
    }

}
